import Vue from 'vue'
import VueRouter from 'vue-router'
import local from '@/utils/local.js'
import { Message } from 'element-ui'
Vue.use(VueRouter)

const routes = [
  //默认打开路径为登录页
  { path: '/', redirect: '/login' },
  //登录页
  { path: '/login', component: () => import('@/views/login/Login.vue') },
  //后台首页
  {
    path: '/index',
    component: () => import('@/views/index/Layout.vue'),
    meta: { title: '后台首页' },
    children: [{ path: '', component: () => import('@/views/index/Index.vue') }]
  },
  //账号管理
  {
    path: '/account',
    component: () => import('@/views/index/Layout.vue'), redirect: '/account/list',
    meta: { title: '账号管理' },
    children: [
      { path: '/account/list', meta: { title: '账号列表' }, component: () => import('@/views/account/AccountList.vue') },
      { path: '/account/add', meta: { title: '账号添加' }, component: () => import('@/views/account/AccountAdd.vue') },
      { path: '/account/changepassword', meta: { title: '修改密码' }, component: () => import('@/views/account/ChangePassword.vue') },
      { path: '/account/personal', meta: { title: '管理员信息' }, component: () => import('@/views/account/Personal.vue') }
    ]
  },
  //商品管理
  {
    path: '/goods', meta: { title: '商品管理' },
    component: () => import('@/views/index/Layout.vue'), redirect: '/goods/list',
    children: [
      { path: '/goods/list', meta: { title: '商品列表' }, component: () => import('@/views/goods/GoodsList.vue') },
      { path: '/goods/add', meta: { title: '商品添加' }, component: () => import('@/views/goods/GoodsAdd.vue') },
      { path: '/goods/category', meta: { title: '商品分类' }, component: () => import('@/views/goods/GoodsCategory.vue') },
      { path: '/goods/edit', meta: { title: '商品修改' }, component: () => import('@/views/goods/GoodsEdit.vue') },
    ]
  },
  //销售统计
  {
    path: '/count', meta: { title: '销售统计' },
    component: () => import('@/views/index/Layout.vue'),
    redirect: '/count/goods',
    children: [
      { path: '/count/goods', meta: { title: '商品统计' }, component: () => import('@/views/count/CountGoods.vue') },
      { path: '/count/order', meta: { title: '订单统计' }, component: () => import('@/views/count/CountOrder.vue') },
    ]
  },
  //订单管理
  {
    path: '/order', meta: { title: '订单管理' },
    component: () => import('@/views/index/Layout.vue'),
    children: [
      { path: '', component: () => import('@/views/order/Order.vue') }
    ]
  },
  //店铺管理
  {
    path: '/shop', meta: { title: '店铺管理' },
    component: () => import('../views/index/Layout.vue'),
    children: [
      { path: '', component: () => import('@/views/shop/Shop.vue') }
    ]
  },

]

//防止重复跳转页面时报错
// 获取原型对象push函数
const originalPush = VueRouter.prototype.push
// 修改原型对象中的push函数 
VueRouter.prototype.push = function push (location) { return originalPush.call(this, location).catch(err => err) }
const router = new VueRouter({
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
  //登录页面自动放行
  if (to.path === '/login') {
    next()
  } else {
    let token = local.get('token')
    if (token) {
      next();//存在token 直接放行
    } else {
      Message.error('登录以后再操作');
      next({ path: '/login' })
    }
  }
})

export default router
